package com.sunther.document.controller;

import cn.hutool.jwt.JWT;
import com.sunther.document.entity.User;
import com.sunther.document.service.UserService;
import com.sunther.idb.common.util.WebResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @author csb
 * @description: TODO
 * @date 2023/5/6 9:43
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/list")
    public WebResult list(@RequestParam(required = false) String keyword) {
        try {
            return WebResult.success("操作成功",userService.listByKeyword(keyword));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }

    }

    @PostMapping("/save")
    public WebResult saveOrUpdateUser(@RequestBody User user,HttpServletRequest request) {
        try {
            return userService.saveOrUpdateUser(user, request);
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @GetMapping("/page_list")
    public WebResult pageList(@RequestParam(required = false) String keyword,
                              @RequestParam(required = false) String projectId,
                              @RequestParam(required = false) Integer roleId,
                              @RequestParam int pageNumber,
                              @RequestParam int pageSize) {
        try {
            return WebResult.success(userService.pageList(keyword,projectId,roleId,pageNumber,pageSize));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }

    @GetMapping("/export")
    public void export(@RequestParam String keyword,
                       @RequestParam(required = false) String projectId,
                       @RequestParam(required = false) Integer roleId,
                       HttpServletResponse response) throws Exception {
        userService.export(keyword,projectId,roleId,response);
    }

    @PostMapping("/reset_password")
    public WebResult resetPassword(@RequestParam Integer id,
                                   @RequestParam String newPassword){
        return userService.resetPassword(id,newPassword);
    }

    @GetMapping("/dtl")
    public WebResult dtl(@RequestParam Integer id) {
        try {
            return WebResult.success("操作成功",userService.dtl(id));
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }

    }

    @GetMapping("/delete")
    public WebResult delete(@RequestParam(value = "id") String id,
                            HttpServletRequest request) {
        try {
            return userService.delete(id);
        }catch (Exception e) {
            return WebResult.failure("操作失败");
        }
    }
}
